import Flutter
import UIKit
import MapManager
import AMapFoundationKit
import AMapLocationKit

public class SwiftLocationUploadFlutterPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "location_upload_flutter", binaryMessenger: registrar.messenger())
    let instance = SwiftLocationUploadFlutterPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
      if (call.method == "setApiKey") {
          let arguments:Dictionary = call.arguments as! Dictionary<String, String>
          let apiKey:String = arguments["ios"]  ?? ""
          
          if apiKey.count == 0 {
              result(false)
          } else {
              AMapServices.shared().apiKey = apiKey
          }
          
      } else if (call.method == "auth") {
          ///授权认证
          /*
           appId是NSString网络货运企业APP的唯一标识
           2appSecurity是NSString网络货运企业在省平台申请的接入安全码
           3enterpriseSenderCode是NSString网络货运企业在省平台申请的企业发送代码
           4environment是NSString环境:“debug”接入测试环境，“release”接入正式环境。
           5listener是返回结果回调函数
           */
          AMapLocationManager.updatePrivacyAgree(AMapPrivacyAgreeStatus(rawValue: 1)!)
          AMapLocationManager.updatePrivacyShow(AMapPrivacyShowStatus(rawValue: 1)!, privacyInfo: AMapPrivacyInfoStatus(rawValue: 1)!)
          
          let arguments:NSDictionary = call.arguments as! NSDictionary
          MapService().open(withAppId: arguments["appId"] as! String, appSecurity: arguments["appSecurity"] as! String, enterpriseSenderCode: arguments["enterpriseSenderCode"] as! String, environment: arguments["environment"] as! String) { model in
              result(model)
          }
      } else if (call.method == "start") {
          ///开启定位
          /*
           vehicleNumber是NSString车牌号
           2driverName是NSString司机姓名
           3shippingNoteInfos是NSArray运单信息列表
           4listener是结果回调函数
           */
          AMapLocationManager.updatePrivacyAgree(AMapPrivacyAgreeStatus(rawValue: 1)!)
          AMapLocationManager.updatePrivacyShow(AMapPrivacyShowStatus(rawValue: 1)!, privacyInfo: AMapPrivacyInfoStatus(rawValue: 1)!)
          
          let arguments:NSDictionary = call.arguments as! NSDictionary
          MapService().startLocation(withShippingNoteInfos: arguments["shippingNoteInfos"] as! [Any], driverNameView: arguments["driverName"] as! String , vehicleNumberView: arguments["vehicleNumber"] as! String, remark: arguments["remark"] as! String) { model in
              result(model)
          }
      } else if (call.method == "send") {
          ///位置信息上报
          /*
           vehicleNumber是NSString车牌号
           2driverName是NSString司机姓名
           3shippingNoteInfos是NSArray运单信息列表
           4listener是结果回调函数
           */
          
          AMapLocationManager.updatePrivacyAgree(AMapPrivacyAgreeStatus(rawValue: 1)!)
          AMapLocationManager.updatePrivacyShow(AMapPrivacyShowStatus(rawValue: 1)!, privacyInfo: AMapPrivacyInfoStatus(rawValue: 1)!)
          let arguments:NSDictionary = call.arguments as! NSDictionary
          MapService().startLocation(withShippingNoteInfos: arguments["shippingNoteInfos"] as! [Any], driverNameView: arguments["driverName"] as! String , vehicleNumberView: arguments["vehicleNumber"] as! String, remark: arguments["remark"] as! String) { model in
              result(model)
              
          }
      } else if (call.method == "pause") {
          ///暂停定位
          /*
           vehicleNumber是NSString车牌号
           2driverName是NSString司机姓名
           3shippingNoteInfos是NSArray运单信息列表
           4listener是结果回调函数
           */
          AMapLocationManager.updatePrivacyAgree(AMapPrivacyAgreeStatus(rawValue: 1)!)
          AMapLocationManager.updatePrivacyShow(AMapPrivacyShowStatus(rawValue: 1)!, privacyInfo: AMapPrivacyInfoStatus(rawValue: 1)!)
          let arguments:NSDictionary = call.arguments as! NSDictionary
          
          MapService().pauseLocation(withShippingNoteInfos: arguments["shippingNoteInfos"] as! [Any], driverNameView: arguments["driverName"] as! String , vehicleNumberView: arguments["vehicleNumber"] as! String, remark: arguments["remark"] as! String) { model in
              result(model)
          }
      } else if (call.method == "restart") {
          ///暂停定位
          /*
           vehicleNumber是NSString车牌号
           2driverName是NSString司机姓名
           3shippingNoteInfos是NSArray运单信息列表
           4listener是结果回调函数
           */
          
          AMapLocationManager.updatePrivacyAgree(AMapPrivacyAgreeStatus(rawValue: 1)!)
          AMapLocationManager.updatePrivacyShow(AMapPrivacyShowStatus(rawValue: 1)!, privacyInfo: AMapPrivacyInfoStatus(rawValue: 1)!)
          let arguments:NSDictionary = call.arguments as! NSDictionary
          
          MapService().reStartLocation(withShippingNoteInfos: arguments["shippingNoteInfos"] as! [Any], driverNameView: arguments["driverName"] as! String , vehicleNumberView: arguments["vehicleNumber"] as! String, remark: arguments["remark"] as! String) { model in
              result(model)
          }
      } else if (call.method == "stop") {
          ///结束定位
          /*
           vehicleNumber是NSString车牌号
           2driverName是NSString司机姓名
           3shippingNoteInfos是NSArray运单信息列表
           4listener是结果回调函数
           */
          
          AMapLocationManager.updatePrivacyAgree(AMapPrivacyAgreeStatus(rawValue: 1)!)
          AMapLocationManager.updatePrivacyShow(AMapPrivacyShowStatus(rawValue: 1)!, privacyInfo: AMapPrivacyInfoStatus(rawValue: 1)!)
          
          let arguments:NSDictionary = call.arguments as! NSDictionary
          
          MapService().stopLocation(withShippingNoteInfos: arguments["shippingNoteInfos"] as! [Any], driverNameView: arguments["driverName"] as! String , vehicleNumberView: arguments["vehicleNumber"] as! String, remark: arguments["remark"] as! String) { model in
              result(model)
          }
      } else {
          result("iOS " + UIDevice.current.systemVersion)
      }
  }
}
